package com.ilibrary.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;

import com.ilibrary.base.MyBaseVO;
import com.ilibrary.constants.MyMsgConstants;
import com.ilibrary.db.dao.hibernate.BookDao;
import com.ilibrary.db.dao.hibernate.CategoryDao;
import com.ilibrary.db.entity.Book;
import com.ilibrary.db.entity.Category;
import com.ilibrary.service.BookService;
import com.ilibrary.utils.MyColumnIndicator;
import com.ilibrary.web.dto.ResultDto;
import com.ilibrary.web.vo.BookVO;

public class BookServiceImpl implements BookService {
	private BookDao bookDao;
	private CategoryDao categoryDao;

	public BookDao getBookDao() {
		return bookDao;
	}
	public void setBookDao(BookDao bookDao) {
		this.bookDao = bookDao;
	}
	public CategoryDao getCategoryDao() {
		return categoryDao;
	}
	public void setCategoryDao(CategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}
	
	@Override
	public ResultDto<MyBaseVO> add(BookVO bookVO) {
		ResultDto<MyBaseVO> resultDto = new ResultDto<MyBaseVO>();
		Book book = new Book();
		try {
			BeanUtils.copyProperties(book, bookVO);
			Category category = categoryDao.findById(bookVO.getCategoryId());
			book.setCategory(category);
		} catch (IllegalAccessException e) {
			resultDto.setSuccess(false);
			resultDto.setMsg(MyMsgConstants.ADD_EXCEPTION);
			e.printStackTrace();
			return resultDto;
		} catch (InvocationTargetException e) {
			resultDto.setSuccess(false);
			resultDto.setMsg(MyMsgConstants.ADD_EXCEPTION);
			e.printStackTrace();
			return resultDto;
		}
		bookDao.add(book);
		resultDto.setSuccess(true);
		return resultDto;
	}
	@Override
	public ResultDto<BookVO> findAll() {
		ResultDto<BookVO> resultDto = new ResultDto<BookVO>();
		BookVO bookVO = null;
		Category tempCategory = null;
		
		List<Book> bookList = bookDao.findAll();
		
		for (Book book : bookList) {
			bookVO = new BookVO();
			try {
				BeanUtils.copyProperties(bookVO, book);
				tempCategory = categoryDao.findById(book.getCategory().getCategoryId());
				bookVO.setCategoryId(tempCategory.getCategoryId());
				bookVO.setCategoryName(tempCategory.getCategoryName());
				resultDto.addToResultList(bookVO);
			} catch (IllegalAccessException e) {
				resultDto.setSuccess(false);
				resultDto.setMsg(MyMsgConstants.ADD_EXCEPTION);
				e.printStackTrace();
				return resultDto;
			} catch (InvocationTargetException e) {
				resultDto.setSuccess(false);
				resultDto.setMsg(MyMsgConstants.ADD_EXCEPTION);
				e.printStackTrace();
				return resultDto;
			}
		}
		resultDto.setSuccess(true);
		return resultDto;
	}
	
	@Override
	public ResultDto<BookVO> queryList(int start, int limit, String categoryId, String keywords) {
		ResultDto<BookVO> resultDto = new ResultDto<BookVO>();
		BookVO bookVO = null;
		Category tempCategory = null;
		
		List<Book> bookList = bookDao.queryList(start, limit, categoryId, keywords);
		Integer total = bookDao.queryCnt(categoryId, keywords);
		
		for (Book book : bookList) {
			bookVO = new BookVO();
			try {
				BeanUtils.copyProperties(bookVO, book);
				tempCategory = categoryDao.findById(book.getCategory().getCategoryId());
				bookVO.setCategoryId(tempCategory.getCategoryId());
				bookVO.setCategoryName(tempCategory.getCategoryName());
				resultDto.addToResultList(bookVO);
			} catch (IllegalAccessException e) {
				resultDto.setSuccess(false);
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				resultDto.setSuccess(false);
				e.printStackTrace();
			}
		}
		resultDto.setTotal(total);
		resultDto.setSuccess(true);
		return resultDto;
	}
	
	@Override
	public ResultDto<BookVO> findById(int bookId) {
		ResultDto<BookVO> resultDto = new ResultDto<BookVO>();
		Book book = bookDao.findById(bookId);
		BookVO bookVO = new BookVO();
		Category tempCategory = null;
		try {
			BeanUtils.copyProperties(bookVO, book);
			tempCategory = categoryDao.findById(book.getCategory().getCategoryId());
			bookVO.setCategoryId(tempCategory.getCategoryId());
			bookVO.setCategoryName(tempCategory.getCategoryName());
			if(book.getBookStatus() == MyColumnIndicator.BOOK_STATUS_IN_LIB) {
				bookVO.setBookStatusString(MyColumnIndicator.BOOK_STATUS_IN_LIB_STRING);
			}
			if(book.getBookStatus() == MyColumnIndicator.BOOK_STATUS_BE_LOAN) {
				bookVO.setBookStatusString(MyColumnIndicator.BOOK_STATUS_BE_LOAN_STRING);
			}
			if(book.getBookStatus() == MyColumnIndicator.BOOK_STATUS_BE_LOSE) {
				bookVO.setBookStatusString(MyColumnIndicator.BOOK_STATUS_BE_LOSE_STRING);
			}
			resultDto.addToResultList(bookVO);
		} catch (IllegalAccessException e) {
			resultDto.setSuccess(false);
			resultDto.setMsg(MyMsgConstants.ADD_EXCEPTION);
			e.printStackTrace();
			return resultDto;
		} catch (InvocationTargetException e) {
			resultDto.setSuccess(false);
			resultDto.setMsg(MyMsgConstants.ADD_EXCEPTION);
			e.printStackTrace();
			return resultDto;
		}
		resultDto.setSuccess(true);
		return resultDto;
	}
	
	@Override
	public ResultDto<BookVO> queryListByKeyword(int start, int limit, String keywords) {
		ResultDto<BookVO> resultDto = new ResultDto<BookVO>();
		BookVO bookVO = null;
		Category tempCategory = null;
		
		List<Book> bookList = bookDao.queryListByKeyword(start, limit, keywords);
		Integer total = bookDao.queryCntByKeyword(keywords);
		
		for (Book book : bookList) {
			bookVO = new BookVO();
			try {
				BeanUtils.copyProperties(bookVO, book);
				tempCategory = categoryDao.findById(book.getCategory().getCategoryId());
				bookVO.setCategoryId(tempCategory.getCategoryId());
				bookVO.setCategoryName(tempCategory.getCategoryName());
				if(book.getBookStatus() == MyColumnIndicator.BOOK_STATUS_IN_LIB) {
					bookVO.setBookStatusString(MyColumnIndicator.BOOK_STATUS_IN_LIB_STRING);
				}
				if(book.getBookStatus() == MyColumnIndicator.BOOK_STATUS_BE_LOAN) {
					bookVO.setBookStatusString(MyColumnIndicator.BOOK_STATUS_BE_LOAN_STRING);
				}
				if(book.getBookStatus() == MyColumnIndicator.BOOK_STATUS_BE_LOSE) {
					bookVO.setBookStatusString(MyColumnIndicator.BOOK_STATUS_BE_LOSE_STRING);
				}
				resultDto.addToResultList(bookVO);
			} catch (IllegalAccessException e) {
				resultDto.setSuccess(false);
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				resultDto.setSuccess(false);
				e.printStackTrace();
			}
		}
		resultDto.setTotal(total);
		resultDto.setSuccess(true);
		return resultDto;
	}
	@Override
	public ResultDto<MyBaseVO> update(BookVO bookVO) {
		ResultDto<MyBaseVO> resultDto = new ResultDto<MyBaseVO>();
		Category category = categoryDao.findById(bookVO.getCategoryId());
		Book book = new Book();
		try {
			BeanUtils.copyProperties(book, bookVO);
			book.setCategory(category);
			bookDao.update(book);
			resultDto.setSuccess(true);
		} catch (Exception e) {
			resultDto.setSuccess(false);
			e.printStackTrace();
		}
		return resultDto;
	}
	
}
